package com.ruoyi.damage.service.impl;

import java.util.List;

import com.ruoyi.apparatus.domain.AlgsExperimentalApparatus;
import com.ruoyi.apparatus.mapper.AlgsExperimentalApparatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.damage.mapper.AlgsEquipmentDamageMapper;
import com.ruoyi.damage.domain.AlgsEquipmentDamage;
import com.ruoyi.damage.service.IAlgsEquipmentDamageService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 器材损坏记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-20
 */
@Service
public class AlgsEquipmentDamageServiceImpl implements IAlgsEquipmentDamageService 
{
    @Autowired
    private AlgsEquipmentDamageMapper algsEquipmentDamageMapper;

    @Autowired
    private AlgsExperimentalApparatusMapper algsExperimentalApparatusMapper;

    /**
     * 查询器材损坏记录
     * 
     * @param equipmentDamageId 器材损坏记录主键
     * @return 器材损坏记录
     */
    @Override
    public AlgsEquipmentDamage selectAlgsEquipmentDamageByEquipmentDamageId(Long equipmentDamageId)
    {
        return algsEquipmentDamageMapper.selectAlgsEquipmentDamageByEquipmentDamageId(equipmentDamageId);
    }

    /**
     * 查询器材损坏记录列表
     * 
     * @param algsEquipmentDamage 器材损坏记录
     * @return 器材损坏记录
     */
    @Override
    public List<AlgsEquipmentDamage> selectAlgsEquipmentDamageList(AlgsEquipmentDamage algsEquipmentDamage)
    {
        return algsEquipmentDamageMapper.selectAlgsEquipmentDamageList(algsEquipmentDamage);
    }

    /**
     * 新增器材损坏记录
     * 
     * @param algsEquipmentDamage 器材损坏记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertAlgsEquipmentDamage(AlgsEquipmentDamage algsEquipmentDamage) throws Exception {
         Long equipmentDamageNum = algsEquipmentDamage.getEquipmentDamageNum();
        Long experimentalApparatusId = algsEquipmentDamage.getExperimentalApparatusId();
        AlgsExperimentalApparatus algsExperimentalApparatus = algsExperimentalApparatusMapper.selectAlgsExperimentalApparatusByExperimentalApparatusId(experimentalApparatusId);
         Long equipmentDamageNum1 = algsExperimentalApparatus.getExperimentalApparatusNum();
         if(equipmentDamageNum1-equipmentDamageNum<0){
                 throw new Exception("损坏数量超出库存，请重新输入");

         }
        algsExperimentalApparatus.setExperimentalApparatusNum(equipmentDamageNum1-equipmentDamageNum);
         System.out.println(algsExperimentalApparatus);
     algsExperimentalApparatusMapper.updateAlgsExperimentalApparatus(algsExperimentalApparatus);

        return algsEquipmentDamageMapper.insertAlgsEquipmentDamage(algsEquipmentDamage);
    }

    /**
     * 修改器材损坏记录
     * 
     * @param algsEquipmentDamage 器材损坏记录
     * @return 结果
     */
    @Override
    public int updateAlgsEquipmentDamage(AlgsEquipmentDamage algsEquipmentDamage)
    {
        return algsEquipmentDamageMapper.updateAlgsEquipmentDamage(algsEquipmentDamage);
    }

    /**
     * 批量删除器材损坏记录
     * 
     * @param equipmentDamageIds 需要删除的器材损坏记录主键
     * @return 结果
     */
    @Override
    public int deleteAlgsEquipmentDamageByEquipmentDamageIds(Long[] equipmentDamageIds)
    {
        return algsEquipmentDamageMapper.deleteAlgsEquipmentDamageByEquipmentDamageIds(equipmentDamageIds);
    }

    /**
     * 删除器材损坏记录信息
     * 
     * @param equipmentDamageId 器材损坏记录主键
     * @return 结果
     */
    @Override
    public int deleteAlgsEquipmentDamageByEquipmentDamageId(Long equipmentDamageId)
    {
        return algsEquipmentDamageMapper.deleteAlgsEquipmentDamageByEquipmentDamageId(equipmentDamageId);
    }
}
